<?php
/**
 * WebProduction Shop (wpshop)
 * @copyright (C) 2011-2012 WebProduction (tm) <webproduction.com.ua>
 *
 * This program is commercial software; you cannot redistribute it and/or
 * modify.
 */

/**
 * @author Maxim Miroshnichenko <max@webproduction.com.ua>
 * @copyright WebProduction
 * @package Shop
 */
class Shop_ContentTable extends Forms_ContentTable {

    public function __construct($datasource) {
        parent::__construct($datasource);
        $this->getStepper()->setFileHTML(dirname(__FILE__).'/Shop_ContentTable_Stepper.html');
        $this->setFileHTML(dirname(__FILE__).'/Shop_ContentTable.html');
        $this->setCSSClassName('bc-table');
    }

    public function disableExports() {
        $this->_disableExports = true;
    }

    public function render() {
        $this->setValue('disableExports', $this->_disableExports);

        $result = parent::render();

        // exports
        $exportCSV = Engine::GetURLParser()->getArgumentSecure('export-csv');
        $exportXML = Engine::GetURLParser()->getArgumentSecure('export-xml');
        $exportXLS = Engine::GetURLParser()->getArgumentSecure('export-xls');

        // export selected data to CSV
        if (!$this->_disableExports && ($exportCSV || $exportXLS || $exportXML)) {
            $data = $this->makeDataSourceData(
            true, // with filters
            true, // with sorts
            false, // without pages
            false // without count
            );

            $filename = 'export '.date('Y-m-d H:i:s').' '.get_class($this->getDataSource());
            $filename = str_replace('DataSource_', '', $filename);
            $filename = str_replace('Datasource_', '', $filename);

            if ($exportCSV) {
                PackageLoader::Get()->import('CSV');
                $csv = CSV_Creator::CreateFromArray($data);
                header('Content-type: text/csv');
                header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
                print $csv->__toString();
                exit();
            }

            if ($exportXLS) {
                PackageLoader::Get()->import('XLS');
                $xls = XLS_Creator::CreateFromArray($data);
                header('Content-type: application/vnd.ms-excel');
                header('Content-Disposition: attachment; filename="'.$filename.'.xls"');
                print $xls->__toString();
                exit();
            }

            if ($exportXML) {
                PackageLoader::Get()->import('XML');
                $xml = XML_Creator::CreateFromArray($data);
                header('Content-type: text/xml');
                header('Content-Disposition: attachment; filename="'.$filename.'.xml"');
                print $xml->__toString();
                exit();
            }
        }

        return $result;
    }

    /**
     * Получить количество строк на одной странице
     *
     * @return int
     */
    public function getLinesOnPage() {
        // пытаемся достать параметр из COOKIE
        $rowsCount = (int) @$_COOKIE['rowscount_'.get_class($this->getDataSource())];
        if (!$rowsCount && $rowsCount <= 0) {
            return 50;
        }
        if ($rowsCount <= 10) {
            $rowsCount = 10;
        }
        if ($rowsCount >= 10000) {
            $rowsCount = 10000;
        }
        return $rowsCount;
    }



    private $_disableExports = false;
}